package com.gfl.trivialrestclient.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author student
 */
@Entity
@Table(name = "PRODUCTS")
@XmlRootElement(name = "product")
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PRODUCT_ID", nullable = false)
    private Long id;
    @Column(name = "NAME", length = 255, nullable = false)
    private String name;
    @Column(name = "UNITS", nullable = false)
    private Integer units;
    @Column(name = "WEIGHT", nullable = false)
    private Double weight;
    @Column(name = "ADDITION_DATE", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date additionDate;

    protected Product() {
    }

    public Product(Product product) {
        if (product == null) {
            return;
        }

        id = product.getId();
        name = product.getName();
        units = product.getUnits();
        weight = product.getWeight();
        additionDate = product.getAdditionDate() != null ? new Date(product.getAdditionDate().getTime()) : null;;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getUnits() {
        return units;
    }

    public void setUnits(Integer units) {
        this.units = units;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

    @XmlElement(type = Date.class)
    public Date getAdditionDate() {
        return additionDate;
    }

    public void setAdditionDate(Date additionDate) {
        this.additionDate = additionDate;
    }

    public static class Builder {

        private Product product;

        public Builder() {
            product = new Product();
        }

        public Builder id(Long id) {
            product.setId(id);
            return this;
        }

        public Builder name(String name) {
            product.setName(name);
            return this;
        }

        public Builder units(Integer units) {
            product.setUnits(units);
            return this;
        }

        public Builder weight(Double weight) {
            product.setWeight(weight);
            return this;
        }

        public Builder added(Date date) {
            product.setAdditionDate(date);
            return this;
        }

        public Builder addedNow() {
            product.setAdditionDate(new Date());
            return this;
        }

        public Product build() {
            return new Product(product);
        }
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 11 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 11 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 11 * hash + (this.units != null ? this.units.hashCode() : 0);
        hash = 11 * hash + (this.weight != null ? this.weight.hashCode() : 0);
        hash = 11 * hash + (this.additionDate != null ? this.additionDate.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.units != other.units && (this.units == null || !this.units.equals(other.units))) {
            return false;
        }
        if (this.weight != other.weight && (this.weight == null || !this.weight.equals(other.weight))) {
            return false;
        }
        if (this.additionDate != other.additionDate && (this.additionDate == null || !this.additionDate.equals(other.additionDate))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Product{" + "id=" + id + ", name=" + name + ", units=" + units + ", weight=" + weight + ", additionDate=" + additionDate + '}';
    }
}
